#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    isUntitle=true;
    curfile=tr("未命名.txt");
    this->setWindowTitle(curfile);
}

MainWindow::~MainWindow()
{
    delete ui;


}

void MainWindow::on_actionOpen_O_triggered()
{
  if(isSave())
  {
      QString filename=QFileDialog::getOpenFileName(this);
      if(!filename.isEmpty())
      {
          ui->textEdit->setVisible(true);
      }
  }
}

void MainWindow::on_actionSave_S_triggered()
{
  Save();
}

void MainWindow::on_actionSave_as_A_triggered()
{
  SaveAs();
}

void MainWindow::on_actionNews_N_triggered()
{
    NewFiles();
}

void MainWindow::NewFiles(){


    if(isSave())
    {
        isUntitle=true;
        curfile=tr("未命名.txt");
        setWindowTitle(curfile);
        ui->textEdit->clear();
        ui->textEdit->setVisible(true);
    }
}
bool MainWindow::Save(){

    if(isUntitle)
    {
        return SaveAs();
    }
    else{

        return SaveFile(curfile);
    }

}

bool MainWindow::SaveAs(){

    QString  filename=tr("");
    filename=QFileDialog::getSaveFileName(this,tr("另存为\n"),curfile);
    if(filename.isEmpty())
    {
        return false;
    }
    else{
        return SaveFile(filename);
    }

}
bool MainWindow::isSave(){

    if(ui->textEdit->document()->isModified())
    {
        QMessageBox box;
        box.setWindowTitle(tr("警告"));
        box.setIcon(QMessageBox::Warning);
        box.setText(curfile+tr("尚未保存，是否保存？"));
        QPushButton *btn=box.addButton(tr("是\n"),QMessageBox::YesRole);
        box.addButton(tr("否(&N)"),QMessageBox::NoRole);
        QPushButton *btnc=box.addButton(tr("取消"),QMessageBox::RejectRole);
        box.exec();
        if(box.clickedButton()==btn)
        {
            return Save();
        }
        else if(box.clickedButton()==btnc)
        {
             return false;
        }

    }

    return  isUntitle;
}

bool  MainWindow::SaveFile(const QString  &filename){

   QFile  file(filename);
   if(!file.open(QFile::WriteOnly|QFile::Text))
   {
       QMessageBox::warning(this,tr("多文件编辑器"),tr("无法写入文件%1， /n 52").arg(filename,file.errorString()));
   }
   QTextStream out(&file);
   out<<ui->textEdit->toPlainText();
   isUntitle=false;
   curfile=QFileInfo(filename).canonicalFilePath();
   setWindowTitle(curfile);
   return  true;
}

bool  MainWindow::LoadFile(const QString &filename){

    QFile file(filename);
    if(!file.open(QFile::ReadOnly|QFile::Text))
    {
        QMessageBox::warning(this,tr("多文档编辑器"),

        tr("无法读取文件 %1 /n  %2").arg(filename).arg(file.errorString()));
        return false;
    }
    QTextStream in(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    ui->textEdit->setPlainText(in.readAll());
    QApplication::setOverrideCursor(Qt::CustomCursor);
    curfile=QFileInfo(filename).canonicalFilePath();
    return true;
}

void MainWindow::on_actionClose_C_triggered()
{
    if(isSave())
    {
        ui->textEdit->setVisible(true);
    }
}

void MainWindow::on_actionback_triggered()
{
    ui->textEdit->undo();
}

void MainWindow::on_actioncopy_c_triggered()
{
    ui->textEdit->cut();
}


void MainWindow::on_actionPaste_V_triggered()
{
    ui->textEdit->paste();
}

void  MainWindow::closeEvent(QCloseEvent *event)
{
    if(isSave())
    {
        event->accept();
    }
    else
    {
        event->ignore();
    }
}
